<template>
    <div>
        <c-header :p-title="'管理客商信息录入员'">
            <div class="d-flex justify-content-end align-items-center">
                <a class="btn btn-sm btn-dark m-r-sm"  v-bind:href='downloadFile' v-bind:download="template_file">下载客商信息录入员模板</a>
                <button class="btn btn-sm btn-primary m-r-sm" @click='editStatus("import")'>导入客商信息录入员文档</button>
                <button class="btn btn-sm btn-success m-r-sm"  @click='editDetail()'>添加</button>
                <button type="button" class="btn btn-sm btn-info d-lg-block"  @click="$router.go(-1)"><i class="fa fa-mail-reply m-r-sm"></i>返回</button>
                <btn-refresh class='m-l-15' :p-data='init'></btn-refresh>
            </div>
        </c-header>
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive"  v-if='list'>
                            <table class="vm table color-bordered-table base-bordered-table table-bordered table-hover">
                                <thead>
                                <tr class="nowrap">
                                    <!--<th>ID</th>-->
                                    <th>姓名</th>
                                    <th>电话</th>
                                    <th>公司名称</th>
                                    <th>职位</th>
                                    <!--<th>角色</th>-->
                                    <th>状态</th>
                                    <th width="60">操作</th>
                                </tr>
                                </thead>
                                <tbody  v-show='list.length'>
                                <tr v-for="(item,key) in list">
                                    <!--<td>{{item.id}}</td>-->
                                    <td>{{item.name}}</td>
                                    <td>{{item.phone}}</td>
                                    <td>{{item.organization}}</td>
                                    <td>{{item.position}}</td>
                                    <!--<td><status-label :p-data="item.role_id" p-name='role' :p-far='true'></status-label></td>-->
                                    <td><status-label :p-data="item.status" p-name='worker_status' :p-far="true"></status-label></td>
                                    <td class="no-h-bg">
                                        <div class="table-edit-group">
                                            <Tooltip content="编辑" placement="top">
                                                <button class="btn btn-sm btn-success m-l-sm"  @click='editDetail(item,true)'><i class="fa fa-edit"></i></button>
                                            </Tooltip>
                                            <button v-if="item.status=='0'" class="btn btn-sm btn-success m-l-sm" @click="editStatus('on',item)">启用</button>
                                            <button v-if="item.status=='1'" class="btn btn-sm btn-danger m-l-sm" @click="editStatus('off',item)">禁用</button>

                                            <button v-if="item.status=='-1'" class="btn btn-sm btn-success m-l-sm" @click="editStatus('ok',item)">通过</button>
                                            <button v-if="item.status=='-1'" class="btn btn-sm btn-danger m-l-sm" @click="editStatus('inject',item)">拒绝</button>

                                            <button class="btn btn-sm btn-dark m-l-sm" @click="editStatus('pwd',item)">设置密码</button>
                                        </div>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                            <div  v-show='!list.length' class="text-center m-b-sm">
                                暂无数据！
                            </div>
                        </div>
                        <hr/>
                        <footer class="panel-footer text-right">
                            <CPage :total="search.page_total"
                                   :current='search.page_current'
                                   :page-size='search.page_row'
                                   @on-change='pageCurrentChange'
                                   @on-page-size-change='pageRowChange'></CPage>
                        </footer>
                    </div>
                </div>
            </div>
        </div>
        <operate-inject ref="operate_inject" @on-change="initSubmit"></operate-inject>
        <operate-ok ref="operate_ok" @on-change="initSubmit"></operate-ok>
        <operate-on ref="operate_on" @on-change="initSubmit"></operate-on>
        <operate-off ref="operate_off" @on-change="initSubmit"></operate-off>
        <operate-pwd ref="operate_pwd" @on-change="initSubmit"></operate-pwd>
        <operate-import ref="operate_import" @on-change="initSubmit"></operate-import>

        <operate-save ref="operate_save" @on-change="initSubmit"></operate-save>

    </div>
</template>
<script>
    import serWorkerWrite from '../../serves/api-worker-write'
    import operateInject from './OperateInject'
    import operateOk from './OperateOk'
    import operateOn from './OperateOn'
    import operateOff from './OperateOff'
    import operatePwd from './OperatePwd'
    import operateImport from './OperateImport.vue'
    import operateSave from './save'
    import {img_whq} from 'filters/index'
    const __page_key = 'worker.write.list';
    let searchDef = {

        page_row:'',
        page_current:'',
        page_total:''
    };
    export default{
        components:{
            operateInject,
            operateOk,
            operateOn,
            operateOff,
            operatePwd,
            operateImport,
            operateSave,
        },
        data(){
            return{
                template_file:'',
                list:undefined,
                search:{}
            }
        },
        computed:{
        },
        beforeMount(){
            this.searchReset();
        },
        mounted(){
            this.init();
        },
        methods:{
            searchReset(update,pageState){
                if(pageState){
                    this.$clientPage.def(this.search);
                    return false
                }
                const _p=this.$clientPage.getData(__page_key);
                if(_p){
                    this.$set(this,'search',_p);
                }else{
                    this.$set(this,'search',Object.assign({},searchDef));
                    this.$clientPage.def(this.search);
                }
                if(update){
                    //this.init();
                }
            },
            searchPage(page){
                this.$clientPage.set(this.search,page);
            },
            searchChange(){
                this.$clientPage.def(this.search);
                this.init();
            },
            pageRowChange(num){
                if(num!= this.search.page_row){
                    this.$set(this.search,'page_row',num);
                    if(this.search.page_current == 1){
                        this.init();
                    }
                }
            },
            pageCurrentChange(current){
                if(current!= this.search.page_current){
                    this.$set(this.search,'page_current',current);
                    this.init();
                }
            },
            linkPageChange(name,params,query){
                this.$clientPage.setData(__page_key,Object.assign({},this.search));
                if(name){
                    this.$router.push({name:name,params:params,query:query})
                }
            },
            //------------------------------------------------------------------------
            async init(data){
                let res = await serWorkerWrite.list({
                    page:this.search.page_current,
                    size:this.search.page_row,
                });
                if(res.code==1){
                    this.template_file = res.addtion_data&&res.addtion_data.worker_template;
                    this.list = res.data||[];
                    this.searchPage(res.addtion_data&&res.addtion_data.page_info);
                }else{
                    this.list = [];
                    this.searchReset(undefined,true);
                    this.$Message.error(res.message);
                }
                return true;
            },
            downloadFile(){
                return  img_whq(this.template_file);
            },
            initSubmit(state){
                if(state){
                    this.init()
                }
            },
            editStatus(type,data){
                let modal_name = '';
                if(type=='on'){
                    modal_name = 'operate_on'
                }else if(type=='off'){
                    modal_name = 'operate_off'
                }else if(type=='ok'){
                    modal_name = 'operate_ok'
                }else if(type=='inject'){
                    modal_name = 'operate_inject'
                }else if(type=='pwd'){
                    modal_name = 'operate_pwd'
                }else if(type=='import'){
                    modal_name = 'operate_import';
                    this.$refs[modal_name].handleOpen({});
                    return false
                }
                this.$refs[modal_name].handleOpen({
                    id:data.id
                });
            },
            editDetail(data,edit){
                this.$refs['operate_save'].handleOpen(Object.assign({_type:edit?'edit':'add'},data))
            },
        }
    }
</script>